package org.jeecg.modules.InveotoryCenter.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.InveotoryCenter.entity.chukudanEntity;
import org.jeecg.modules.InveotoryCenter.entity.chukugoodsEntity;
import org.jeecg.modules.InveotoryCenter.entity.piciEntity;
import org.jeecg.modules.InveotoryCenter.mapper.ChukuManagerMapper;
import org.jeecg.modules.InveotoryCenter.mapper.InveotoryQueryMapper;
import org.jeecg.modules.InveotoryCenter.service.ChukuManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

@Service
public class ChukuManagerServiceImpl implements ChukuManagerService {


    @Autowired
    ChukuManagerMapper mapper;


    @Autowired
    InveotoryQueryMapper inveotoryQueryMapper;
    /**
     * 出库申请列表查询
     * @param page
     * @param status
     * @param orderSn
     * @return
     */
    @Override
    public IPage<chukudanEntity> getchukushenqinglist(Page<chukudanEntity> page, int status, String orderSn) {
        return mapper.getchukushenqinglist(page,status,orderSn);
    }

    /**
     * 查询出库申请下的商品
     * @param page
     * @param id
     * @return
     */
    @Override
    public IPage<chukugoodsEntity> getchukushenqingshangping(Page<chukugoodsEntity> page, String id) {

        IPage<chukugoodsEntity> pageList=mapper.getchukushenqingshangping(page,id);

        if(pageList!=null){
            for(int i=0;i<pageList.getRecords().size();i++){
                if(pageList.getRecords().get(i).getGoodsType()==0){
                    HashMap<String,String> map=inveotoryQueryMapper.getGoodsdetailbyId(page.getRecords().get(i).getGoodsId());
                    pageList.getRecords().get(i).setGoodsCode(map.get("goods_code"));
                    pageList.getRecords().get(i).setSupplierGoodsCode(map.get("supplier_goods_code"));
                    pageList.getRecords().get(i).setGoodsTypeName(map.get("goods_type_name"));

                }else {
                    HashMap<String,String> map=inveotoryQueryMapper.getFabricdetailbyId( Integer.parseInt(page.getRecords().get(i).getGoodsId()));
                    pageList.getRecords().get(i).setGoodsCode(map.get("fabric_code"));
                    pageList.getRecords().get(i).setSupplierGoodsCode(map.get("gysfabriccode"));
                    pageList.getRecords().get(i).setGoodsTypeName(map.get("goods_type_name"));
                }
            }
        }

        return pageList;
    }


    /**
     * 查询出库商品下的批次
     * @param page
     * @param id
     * @return
     */
    @Override
    public IPage<piciEntity> getpicibychukugoods(Page<piciEntity> page, String id) {
        IPage<piciEntity> pageList=mapper.getpicibychukugoods(page,id);
        if(pageList!=null){
            for (int i=0;i<pageList.getRecords().size();i++){
                if(pageList.getRecords().get(i).getGoodsType()==0){
                    HashMap<String,String> map=inveotoryQueryMapper.getGoodsdetailbyId(page.getRecords().get(i).getGoodsId());
                    pageList.getRecords().get(i).setUnit(map.get("unit"));
                    pageList.getRecords().get(i).setPrice( new BigDecimal(mapper.getgoodprice(page.getRecords().get(i).getGoodsId())) );
                }else {

                    HashMap<String,String> map=inveotoryQueryMapper.getFabricdetailbyId( Integer.parseInt(page.getRecords().get(i).getGoodsId()));
                    pageList.getRecords().get(i).setUnit(map.get("unit"));
                    pageList.getRecords().get(i).setPrice( new BigDecimal( mapper.getfabricprice(  Integer.parseInt(page.getRecords().get(i).getGoodsId()) ) ));
                }
            }
        }
        return pageList;
    }

    /**
     * 查询商品下的出库明细
     * @param page
     * @param id
     * @return
     */
    @Override
    public IPage<piciEntity> getchukumingxibychukugoods(Page<piciEntity> page, String id) {




        IPage<piciEntity> pageList=mapper.getchukumingxibychukugoods( page, id);

        if(pageList!=null){
            for (int i=0;i<pageList.getRecords().size();i++){
                if(pageList.getRecords().get(i).getGoodsType()==0){
                    HashMap<String,String> map=inveotoryQueryMapper.getGoodsdetailbyId(page.getRecords().get(i).getGoodsId());

                    pageList.getRecords().get(i).setGoodsTypeName(map.get("goods_type_name"));
                    pageList.getRecords().get(i).setUnit(map.get("unit"));
                    pageList.getRecords().get(i).setPrice( new BigDecimal(mapper.getgoodprice(page.getRecords().get(i).getGoodsId())) );
                }else {

                    HashMap<String,String> map=inveotoryQueryMapper.getFabricdetailbyId( Integer.parseInt(page.getRecords().get(i).getGoodsId())  );

                    pageList.getRecords().get(i).setGoodsTypeName(map.get("goods_type_name"));
                    pageList.getRecords().get(i).setUnit(map.get("unit"));
                    pageList.getRecords().get(i).setPrice( new BigDecimal( mapper.getfabricprice(  Integer.parseInt(page.getRecords().get(i).getGoodsId()) ) ));
                }
            }
        }
        return pageList;
    }

    /**
     * 出库明细里的确认出库
     * @param goodsId
     * @param id
     * @param outstorageNumber
     * @param batchCode
     * @return
     */
    @Override
    public int MinxiEnterchuku(String goodsId, String id, String outstorageNumber, String batchCode) {


        //先查询老的库存
        String oldkucun=mapper.getoldkucunbygoodsId(goodsId);


     //先查询老的批次库存
        String oldpicikucun=mapper.getoldpicikucun(batchCode);
        //修改批次库存
        mapper.updatepicikucun(String.valueOf( Double.parseDouble(oldpicikucun)-Double.parseDouble(outstorageNumber)),batchCode,new Date());


        //修改库存
      mapper.updatekucxun( String.valueOf( Double.parseDouble(oldkucun)-Double.parseDouble(outstorageNumber)),goodsId);
        return mapper.updatemingxistatus(id,1);
    }

    /**
     * 出库明细里的撤回
     * @param goodsId
     * @param id
     * @param outstorageNumber
     * @return
     */
    @Override
    public int Minxichehui(String goodsId, String id, String outstorageNumber,String batchCode) {
        //先查询老的库存
        String oldkucun=mapper.getoldkucunbygoodsId(goodsId);

        //先查询老的批次库存
        String oldpicikucun=mapper.getoldpicikucun(batchCode);
        //修改批次库存
        mapper.updatepicikucun(String.valueOf( Double.parseDouble(oldpicikucun)+Double.parseDouble(outstorageNumber)),batchCode, new Date());
        //修改库存
        mapper.updatekucxun( String.valueOf( Double.parseDouble(oldkucun)+Double.parseDouble(outstorageNumber)),goodsId);
        return mapper.updatemingxistatus(id,0);
    }

    /**
     * 新增出库明细
     * @param entity
     * @return
     */
    @Override
    public int addchukumingxi(
            piciEntity entity) {

        entity.setCreateTime(new Date());
        entity.setMingxiId(System.currentTimeMillis());
        mapper.addchukumingxi(entity);
        return mapper.updatechukuxiangstatus(entity.getId(),1);
    }

    /**
     *
     * @param goodsId
     * @param goodsType
     * @return
     */
    @Override
    public  HashMap<String,String> getgoodsdetail(String goodsId, int goodsType) {

        HashMap<String,String> map=null;

            if(goodsType==0){
               map=inveotoryQueryMapper.getGoodsdetailbyId(goodsId);
            }else {
                map=inveotoryQueryMapper.getFabricdetailbyId( Integer.parseInt(goodsId));
            }
        return map;
    }
}
